import BaseView from "../../../cfw/mvc/BaseView";
import BaseChannel from "../../base/BaseChannel";
import GameMatrixItemView from "./GameMatrixItemView";
import SDKManager from "../../SDKManager";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import BaseController from "../../../cfw/mvc/BaseController";
import { DIR } from "../../../cfw/tools/Define";


const { ccclass, property } = cc._decorator;

@ccclass
export default class GameMatrixView extends BaseView {

	@property({ type: cc.Node, displayName: "content$VNode" })
	content$VNode: cc.Node = null;

	@property(cc.Prefab)
	prefab: cc.Prefab = null;

	@property
	scale: number = 1;

	@property
	witchExport: number = 0;

	protected model: BaseChannel;

	protected dir: DIR = DIR.UP;

	@property
	isV: number = 1;

	onLoad() {
		this.model = SDKManager.getChannel()
		this.controller = new BaseController()
		this.updateContent()
		GlobalEvent.instance().on(EventName.SORT_AD_DATA, this.updateContent, this)
	}


	updateContent() {
		if (!cc.isValid(this.node)) {
			return;
		}
		if (!SDKManager.getChannel().isSwtichOpen()) {
			return;
		}
		let list = this.model.getAdData()
		// console.log('updateContent  list.length   ', list.length)
		if (list.length > 0) {
			this.node.active = true;
			for (let index = 0; index < list.length; index++) {
				const element = list[index];
				let node = cc.instantiate(this.prefab)
				// node.scale = this.scale;
				// node.width = node.width * this.scale;
				// node.height = node.height * this.scale;
				let comp = this.controller.initView(node, 'GameMatrixItemView', element) as GameMatrixItemView
				if (comp) {
					comp.witchExport = this.witchExport;
				}
				this.content$VNode.addChild(node)
			}
		} else {
			this.node.active = false;
		}
	}

	update() {
		// let pos = this.content$VNode.y;
		if (this.dir == DIR.UP) {
			if (this.isV) {
				let vs = this.content$VNode.parent.height
				let cs = this.content$VNode.height
				if (cs < vs) {
					return;
				}
				this.content$VNode.y += 2;
				let dis = cs - vs;
				if (this.content$VNode.y > dis) {
					this.content$VNode.y = dis;
					this.dir = DIR.DOWN;
				}
			} else {
				let vs = this.content$VNode.parent.width
				let cs = this.content$VNode.width
				if (cs < vs) {
					return;
				}
				this.content$VNode.x -= 2;
				let dis = cs - vs;
				// console.log(' this.content$VNode.x  1111111111', this.content$VNode.x)
				if (this.content$VNode.x < -dis) {
					this.content$VNode.x = -dis;
					this.dir = DIR.DOWN;
				}
			}

		} else {
			if (this.isV) {
				let vs = this.content$VNode.parent.height
				let cs = this.content$VNode.height
				if (cs < vs) {
					return;
				}
				this.content$VNode.y -= 2;
				if (this.content$VNode.y < 0) {
					this.content$VNode.y = 0;
					this.dir = DIR.UP;
				}
			} else {
				let vs = this.content$VNode.parent.width
				let cs = this.content$VNode.width
				if (cs < vs) {
					return;
				}
				this.content$VNode.x += 2;
				// console.log(' this.content$VNode.x  22222222222222', this.content$VNode.x)
				if (this.content$VNode.x > 0) {
					this.content$VNode.x = 0;
					this.dir = DIR.UP;
				}
			}

		}
	}



	onDestroy() {
		super.onDestroy()
		GlobalEvent.instance().off(EventName.SORT_AD_DATA, this.updateContent, this)
	}



}